package com.example.ai.agent.mcp.tool;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

/**
 * 天气预报服务工具
 *
 * @author AI Agent
 */
@Component
@Slf4j
public class WeatherService {

    private static final Logger logger = LoggerFactory.getLogger(WeatherService.class);

    private final RestTemplate restTemplate;

    // 使用和风天气API作为示例，您可以根据需要替换为其他天气API
    private static final String WEATHER_API_URL = "https://devapi.qweather.com/v7/weather/now";
    private static final String GEO_API_URL = "https://geoapi.qweather.com/v2/city/lookup";

    // 注意：实际使用时需要替换为有效的API密钥
    private static final String API_KEY = "YOUR_API_KEY_HERE";

    public WeatherService(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    /**
     * 获取指定城市的天气信息
     *
     * @param request 天气请求对象
     * @return 天气信息描述
     */
    public String getWeather(WeatherRequest request) {
        log.info("WeatherService.request:{}", request);
        String cityName = request.getCityName();
        try {
            // 1. 首先获取城市ID
            String locationUrl = String.format("%s?location=%s&key=%s", GEO_API_URL, cityName, API_KEY);
            GeoResponse geoResponse = restTemplate.getForObject(locationUrl, GeoResponse.class);

            if (geoResponse == null || geoResponse.getCode() != 200 || geoResponse.getLocation() == null || geoResponse.getLocation().isEmpty()) {
                return "无法找到城市: " + cityName;
            }

            String locationId = geoResponse.getLocation().get(0).getId();

            // 2. 获取天气信息
            String weatherUrl = String.format("%s?location=%s&key=%s", WEATHER_API_URL, locationId, API_KEY);
            WeatherResponse weatherResponse = restTemplate.getForObject(weatherUrl, WeatherResponse.class);

            if (weatherResponse == null || weatherResponse.getCode() != 200 || weatherResponse.getNow() == null) {
                return "获取天气信息失败";
            }

            WeatherNow now = weatherResponse.getNow();
            return String.format("%s的天气情况：\n温度: %s℃\n天气: %s\n风向: %s\n风速: %s公里/小时\n湿度: %s%%",
                    cityName, now.getTemp(), now.getText(), now.getWindDir(), now.getWindSpeed(), now.getHumidity());

        } catch (Exception e) {
            logger.error("获取天气信息失败", e);
            return "获取天气信息时发生错误: " + e.getMessage();
        }
    }

    /**
     * 地理位置响应实体
     */
    @JsonIgnoreProperties(ignoreUnknown = true)
    public static class GeoResponse {
        private int code;
        private java.util.List<Location> location;

        public int getCode() {
            return code;
        }

        public void setCode(int code) {
            this.code = code;
        }

        public java.util.List<Location> getLocation() {
            return location;
        }

        public void setLocation(java.util.List<Location> location) {
            this.location = location;
        }
    }

    /**
     * 位置信息实体
     */
    @JsonIgnoreProperties(ignoreUnknown = true)
    public static class Location {
        private String id;
        private String name;

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }

    /**
     * 天气响应实体
     */
    @JsonIgnoreProperties(ignoreUnknown = true)
    public static class WeatherResponse {
        private int code;
        private WeatherNow now;

        public int getCode() {
            return code;
        }

        public void setCode(int code) {
            this.code = code;
        }

        public WeatherNow getNow() {
            return now;
        }

        public void setNow(WeatherNow now) {
            this.now = now;
        }
    }

    /**
     * 当前天气信息实体
     */
    @JsonIgnoreProperties(ignoreUnknown = true)
    public static class WeatherNow {
        private String temp;
        private String text;
        @JsonProperty("windDir")
        private String windDir;
        @JsonProperty("windScale")
        private String windSpeed;
        private String humidity;

        public String getTemp() {
            return temp;
        }

        public void setTemp(String temp) {
            this.temp = temp;
        }

        public String getText() {
            return text;
        }

        public void setText(String text) {
            this.text = text;
        }

        public String getWindDir() {
            return windDir;
        }

        public void setWindDir(String windDir) {
            this.windDir = windDir;
        }

        public String getWindSpeed() {
            return windSpeed;
        }

        public void setWindSpeed(String windSpeed) {
            this.windSpeed = windSpeed;
        }

        public String getHumidity() {
            return humidity;
        }

        public void setHumidity(String humidity) {
            this.humidity = humidity;
        }
    }

    /**
     * 天气请求类
     */
    @JsonIgnoreProperties(ignoreUnknown = true)
    public static class WeatherRequest {
        @JsonProperty("cityName")
        private String cityName;

        public String getCityName() {
            return cityName;
        }

        public void setCityName(String cityName) {
            this.cityName = cityName;
        }
    }
}